﻿// owner 蒋家伟

#include "OwnerPtrUnitTest.h"

// jsdk
#include "UnitTestDataDefine.h"
#include "UnitTestDbgWarnDefine.h"
#include "OwnerPtrUtils.h"

using namespace jsdk;

class Base{
    DEFINE_DRIVER_BASE(Base)
public:
    virtual ~Base() {}
    void fun() const {};
    void funfw() {};
};
class Derived1 : public Base {
    DEFINE_DRIVER_FROM(Derived1, Base)
};
class Derived2 : public Base {};

UNIT_TEST_START(jsdk, OwnerPtrUnitTest)
{
    OwnerPtrUnitTest test;
    bool success = test.testCommon();
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"OwnerPtr通用功能测试失败", L"蒋家伟", L"2023-11-10");
    return true;
}

bool jsdk::OwnerPtrUnitTest::testCommon() const
{
    OwnerPtr<Derived1> opDerived1 = NEW_AS_OWNERPTR(Derived1);
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(opDerived1, L"Derived1指针对象创建失败", L"蒋家伟", L"2023-11-10");
    OwnerPtr<Base> opDerived2 = NEW_AS_OWNERPTR(Derived2);
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(opDerived2, L"opDerived2指针对象创建失败", L"蒋家伟", L"2023-11-10");
    OwnerPtr<Derived1> opCastDerived1 = transformOwnerShipAndCast<Derived1>(opDerived2);
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(opCastDerived1 == nullptr, L"opDerived2 cast opCastDerived1 应该失败而不是成功", L"蒋家伟", L"2023-11-10");
    OwnerPtr<Derived2> opCastDerived2 = transformOwnerShipAndCast<Derived2>(opDerived2);
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(opCastDerived2, L"opDerived2 cast opCastDerived2 应该成功而不是失败", L"蒋家伟", L"2023-11-10");

    OwnerPtr<Base> opBase = NEW_AS_OWNERPTR(Base);
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(opBase, L"Base智能指针创建失败", L"蒋家伟", L"2023-11-10");
    OwnerPtr<Derived1> opCastDerived1_ = transformOwnerShipAndCast<Derived1>(opBase);
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(opCastDerived1_ == nullptr, L"Base智能指针cast Derived1 应该失败但是成功了", L"蒋家伟", L"2023-11-10");

    OwnerPtr<Derived2> opCastDerived2_ = transformOwnerShipAndCast<Derived2>(opBase);
    UNIT_TEST_DBG_WARN_AND_RETURN_FALSE_UNLESS(opCastDerived2_ == nullptr, L"Base智能指针cast Derived2 应该失败但是成功了", L"蒋家伟", L"2023-11-10");

    const OwnerPtr<Base>& opBase_const = opBase;

    opBase_const->fun();
    opBase->fun();
    opBase->funfw();
    return true;
}
